package com.winbomb.simulife.game.screens;

import android.graphics.Color;

import com.winbomb.simulife.Master;
import com.winbomb.simulife.World;
import com.winbomb.simulife.framework.Game;
import com.winbomb.simulife.framework.Screen;
import com.winbomb.simulife.framework.Input.TouchEvent;
import com.winbomb.simulife.game.Assets;
import com.winbomb.simulife.game.GameScreen;
import com.winbomb.simulife.game.TouchArea;
import com.winbomb.simulife.impl.events.HighExamResultEvent;
import com.winbomb.simulife.util.ProbInvalidException;
import com.winbomb.simulife.util.ProbUtil;

public class HighExamScreen extends Screen {

	final TouchArea choiceArea = new TouchArea(6, 140, 287, 280);
	final TouchArea answerArea = new TouchArea(70, 440, 194, 25);
	final TouchArea submitArea = new TouchArea(270, 440, 40, 40);

	private int choiceIdx;
	private static final int QUESTION_NUM = 54;
	private static final int NO_ANSWER = 0; // 没有回答
	/** 问题 */
	private int[] questions;
	/** 答案 */
	private int[] answers;
	/** 作答的题目数 */
	private int answerCnt;

	public HighExamScreen(Game game) {
		super(game);
		questions = raiseQuestions();
		answers = getPreAnswers();
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub

	}

	@Override
	public void present(float deltaTime) {
		g.drawPixmap(Assets.high_exam, 0, 0);

		// draw answers
		for (int i = 0; i < QUESTION_NUM; i++) {
			if (answers[i] == NO_ANSWER) {
				continue;
			}

			int y = (int) (i * 15.5f / 3 + 140);
			int x = (i % 3) * 96 + 6;
			if (answers[i] >= 1 && answers[i] <= 4) {
				x += answers[i] * 19.2f;
			}

			g.drawRect(x, y, 15, 6, Color.BLACK);
		}

		// draw choice
		if (choiceIdx >= 0 && choiceIdx < QUESTION_NUM) {
			g.drawText(String.valueOf(choiceIdx + 1), 25, 450);
			drawAnswer(answers[choiceIdx]);
		}

		// draw submit
		if (answerCnt == QUESTION_NUM) {
			g.drawRect(270, 440, 40, 40, Color.BLUE);
			g.drawText("Submit", 272, 460);
		}
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub

	}

	@Override
	public void update(float deltaTime) {
		updateTouchEvents();

		for (TouchEvent event : touchEvents) {
			if (event.type != TouchEvent.TOUCH_UP) {
				continue;
			}

			if (choiceArea.isBound(event)) {
				choiceIdx = getChoiceIndex(event.x - 6, event.y - 140);
			} else if (answerArea.isBound(event)) {
				int answerIdx = getAnswerIndex(event.x - 70, event.y - 440);
				if (answers[choiceIdx] == NO_ANSWER) {
					answerCnt++;
				}
				answers[choiceIdx] = answerIdx;
			} else if (submitArea.isBound(event)) {
				World world = game.getWorld();
				Master master = world.getMaster();

				// 计算答对的题目数
				int rightAns = 0;
				for (int i = 0; i < QUESTION_NUM; i++) {
					if (answers[i] == questions[i]) {
						rightAns++;
					}
				}

				world.addEvent(new HighExamResultEvent(world));
				world.nextEvent();
				game.setScreen(new GameScreen(game));
			}
		}

	}

	private void drawAnswer(int answerIdx) {
		if (answers[choiceIdx] == NO_ANSWER) {
			return;
		}

		int x = 15 + 55 * answers[choiceIdx];
		g.drawRect(x, 448, 25, 10, Color.BLACK);
	}

	private int getChoiceIndex(int x, int y) {
		int row = y * 9 / 140;
		int col = x * 3 / 288;

		return row * 3 + col;
	}

	private int getAnswerIndex(int x, int y) {
		return x * 4 / 194 + 1;
	}

	private int[] raiseQuestions() {
		int[] questions = new int[QUESTION_NUM];

		// 随机生成A/B/C/D的答案
		float[] probs = new float[] { 0.25f, 0.25f, 0.25f, 0.25f };
		for (int i = 0; i < QUESTION_NUM; i++) {
			try {
				int ques = ProbUtil.testProbs(probs) + 1;
				questions[i] = ques;
			} catch (ProbInvalidException ex) {
				questions[i] = 0;
			}
		}

		return questions;
	}

	private int[] getPreAnswers() {
		int[] answers = new int[QUESTION_NUM];

		for (int i = 0; i < QUESTION_NUM; i++) {
			if (ProbUtil.test(1.0f)) {
				answers[i] = questions[i];
				answerCnt++;
			} else {
				answers[i] = NO_ANSWER;
			}
		}

		return answers;
	}
}
